<?php

namespace app\api\model;

use common\helper\DateHelper;
use common\model\base\BaseModel;
use common\token\Token;
use common\traits\InstanceTrait;

;


/**
 * Class Record
 * @package app\api\model
 */
class Record extends BaseModel
{

    use InstanceTrait;

    protected $pk = 'record_id';

    public static $type_map = [
        1 => ['color' => 'blue', 'type_name' => '知识提升'],
        2 => ['color' => 'green', 'type_name' => '身体提升'],
        3 => ['color' => 'yellow', 'type_name' => '强迫工作'],
        4 => ['color' => 'orange', 'type_name' => '尽情娱乐'],
        5 => ['color' => 'red', 'type_name' => '浪费拖延'],
    ];


    public function paramWhere($param = [])
    {
        $where = $this->getBaseWhere($param);
        if (!empty($param['record_id'])) {
            $where[] = ['record_id', '=', $param['record_id']];
        }
        if (!empty($param['creater_id'])) {
            $where[] = ['creater_id', '=', $param['creater_id']];
        }
        if (!empty($param['exe_date'])) {
            $where[] = ['exe_time', 'between', DateHelper::to_time(DateHelper::day_cycle($param['exe_date']))];
        }
        return $where;
    }

    public function getModel($param = [])
    {
        return Static::where($this->paramWhere($param));
    }

    public function getRecordInfo($param = [], $field = '*')
    {
        $model = $this->getModel($param);
        $data = $model
            ->field($field)
            ->find();
        return $data;
    }

    public function getRecordList($param = [], $field = '*')
    {
        $param['page'] = !empty($param['page']) ? $param['page'] : 1;
        $param['limit'] = !empty($param['limit']) ? $param['limit'] : 20;
        $model = $this->getModel($param);
        $data['count'] = $model->count();
        $model = $this->getModel($param);
        $data['total_page'] = ceil($data['count'] / $param['limit']);
        $data['list'] = $model->limit($param['limit'])
                              ->field($field)
                              ->page($param['page'])
                              ->select();
        return $data;
    }

}
